extends StateBase
class_name StateChaseEnemy01


var enemy01:Enemy01
var enemyAnimations:AnimationPlayer

func enterState(ownerNode):
	pass
	
	
func exitState(ownerNode):
	pass
	
	
func process(delta, ownerNode):
	pass
	
	
func physicsProcess(delta, ownerNode):
	if not enemy01:
		enemy01 = ownerNode as Enemy01
		enemyAnimations = enemy01.animations as AnimationPlayer 
	
	enemyAnimations.play("walk")
	enemy01.velocity.y += enemy01.gravity * enemy01.gravityScale * delta
	
	var player = get_tree().get_first_node_in_group("Player") as PlayerScript
	var distance2Player = player.global_position.distance_to(enemy01.global_position)
	var dir = sign(player.global_position.x - enemy01.global_position.x) 
	
	if distance2Player > enemy01.CHASE_DISTANCE:
		stateMachine.changeState("StateIdle", ownerNode)
	elif distance2Player > enemy01.ATTACK_DISTANCE:
		enemy01.velocity.x = move_toward(enemy01.velocity.x, enemy01.SPEED * dir, enemy01.acceleration * delta)
		enemy01.enemySpriteRoot.scale.x = dir
	else:
		if enemy01.velocity.x != 0: 
			enemy01.velocity.x = 0
		enemyAnimations.play("Idle")
		enemy01.enemySpriteRoot.scale.x = dir
		var randNumber = randf_range(0.0, 1.0)
		if randNumber < 0.01:
			stateMachine.changeState("StateAttack", ownerNode)
		
	enemy01.move_and_slide()

